Ext.define('AM.view.user.UserEdit', {
    extend: 'AM.default.DefaultWindowForm',
    
    requires: ['AM.default.DefaultDateField'],
    
    height: 350,
    width: 280,
    notEdit: true,
    grid: null,
    initComponent: function(){
    	var me = this;
    	this.title = user_lang.editFormInfo.editTitle;
        this.items = [{
            name: 'loginID',
            blankText: user_lang.editFormInfo.loginID_blankText,
            maxLength: 255,
            maxLengthText: user_lang.editFormInfo.loginID_maxLengthText,
            readOnly: true,
            fieldLabel: user_lang.editFormInfo.loginID//账号
        }, {
            name: 'userName',
            blankText: user_lang.editFormInfo.userName_blankText,
            maxLength: 100,
            maxLengthText: user_lang.editFormInfo.userName_maxLengthText,
            fieldLabel: '<span style="color:red;">*</span>' + user_lang.editFormInfo.userName//姓名
        }, {
            xtype: 'combo',
            name: 'sexCode',
            allowBlank: true,
            fieldLabel: user_lang.editFormInfo.sexCode,//性别
            queryMode: 'local',
            store: new Ext.data.ArrayStore({
                fields: ['value', 'text'],
                data: [["1078", user_lang.editFormInfo.sexCode_text[0]], ["1079", user_lang.editFormInfo.sexCode_text[1]]]
            }),
            valueField: 'value',
            displayField: 'text',
            editable: false,
            defaultActive: false,
            emptyText: orchid_lang.pageVerificationInfo.combo_emptyText
        }, /*{
            name: 'password',
            inputType: 'password',
            id: "psd",
            maxLength: 20,
            maxLengthText: user_lang.editFormInfo.password_maxLengthText,
            blankText: user_lang.editFormInfo.password_blankText,
            fieldLabel: '<span style="color:red;">*</span>' + user_lang.editFormInfo.password
        }, {
            name: 'tpassword',
            inputType: 'password',
            // 指定验证规则为两个组件是否相同验证
            vtype: "repetition",
            vtypeText: user_lang.editFormInfo.tpassword_vtypeText,
            // 设置需要判断值的另一个组件ID
            repetition: {
                targetCmpId: 'psd'
            },
            blankText: user_lang.editFormInfo.tpassword_blankText,
            fieldLabel: '<span style="color:red;">*</span>' + user_lang.editFormInfo.tpassword
        }, {
            name: 'email',
            allowBlank: true,
            vtype: 'email',
            fieldLabel: user_lang.editFormInfo.email
        }, */{
            xtype: "combo",
            fieldLabel: '民族',
            name: "folkCode",
            queryMode: 'local',//'remote',
            allowBlank: true,
            store: Ext.create("Ext.data.ArrayStore", {
                singleton: true,
                fields: [{
                    name: 'value',
                    mapping: "dataCode",
                    type: 'string'
                }, {
                    name: 'text',
                    mapping: "dataName",
                    type: 'string'
                }],
                proxy: {
                    type: 'ajax',
                    url: me.grid.store.folkCodeUrl + '?typeCode=1224',
                    reader: Ext.create('Ext.data.reader.Json', {
                        type: 'json',
                        root: 'data',
                        successProperty: 'success'
                    })
                },
                autoLoad: true
            }),
        },{
            xtype: 'defaultDate',
            name: 'birthday',
            allowBlank: true,
            editable: false,
            fieldLabel: user_lang.editFormInfo.birthday//出生日期
        }, {
            name: 'mobileTelephone',
            allowBlank: true,
            vtype: 'phone',
            fieldLabel: user_lang.editFormInfo.mobileTelephone//电话
        }, {
        	fieldLabel: '职务',
        	name: 'dutyRemark',
            allowBlank: true
        }, {
            allowBlank: true,
            xtype: "textareafield",
            name: 'remark',
            maxLength: 1500,
            maxLengthText: user_lang.editFormInfo.remark_maxLengthText,
            fieldLabel: user_lang.editFormInfo.remark//备注
        }, {
            xtype: 'hidden',
            name: 'departmentId'
        }, {
            xtype: 'hidden',
            name: "userId"
        }, {
            xtype: 'hidden',
            name: "activeState"
        }];
        
        this.buttons = [{
            text: orchid_lang.buttons.determineButtonText,
            handler: function(button, event){
                var win = button.up('window');
                var form = win.down('form');
                Ext.Ajax.request({
				url: win.grid.store.inspectCodeUrl,
                    params: {
                        userId: form.getForm().findField("userId").getValue(),
                        loginID: form.getForm().findField("loginID").getValue()
                    },
                    success: function(response){
                        var data = response.responseText;
                        var json = Ext.decode(data, true);
                        if (json.success) {
                            win.sub();
                        }
                        else {
                            Ext.Msg.alert(orchid_lang.promptWindowInfo.newsTitle, json.msg, function(){
                                form.getForm().findField("loginID").focus();
                            });
                        }
                    }
                });
            }
        }, {
            text: orchid_lang.buttons.cancelButtonText,
            scope: this,
            handler: this.close
        }];
        
        this.callParent(arguments);
    },
    sub: function(){
        var win = this;
        var form = win.down('form');
        if (form.getForm().findField("userId").getValue() == "") {
            var baseForm = form.getForm();
            if (!baseForm.isValid()) {
                return;
            }
            win.submit({
			url: win.grid.store.addUrl,
                success: function(f, action){
                    if (action.result.success) {
//                        Ext.Msg.alert('消息', action.result.msg, function(){
                        Ext.Msg.alert(orchid_lang.promptWindowInfo.newsTitle, orchid_lang.promptWindowInfo.addSuccessText, function(){
                            var gridPanel = win.grid;
                            var store = gridPanel.getStore();
                            store.reload();
                            win.close();
                        });
                    }
                },
                failure: function(f, action){
                    if (action.result.msg) {
                        Ext.Msg.alert(orchid_lang.promptWindowInfo.errorTitle, action.result.msg);
                    }
                    else {
                        Ext.Msg.alert(orchid_lang.promptWindowInfo.errorTitle, orchid_lang.promptWindowInfo.addFailureText);
                    }
                }
            });
        }
        else {
            var baseForm = form.getForm();
            if (!baseForm.isValid()) {
                return;
            }
            win.submit({
                url:win.grid.store.updateUrl,
                success: function(f, action){
                    if (action.result.success) {
//                        Ext.Msg.alert('消息', action.result.msg, function(){
                        Ext.Msg.alert(orchid_lang.promptWindowInfo.newsTitle, orchid_lang.promptWindowInfo.updateSuccessText, function(){
                            var gridPanel = win.grid;
                            var store = gridPanel.getStore();
                            store.reload();
                            win.close();
                        });
                    }
                },
                failure: function(f, action){
                    if (action.result.msg) {
                        Ext.Msg.alert(orchid_lang.promptWindowInfo.errorTitle, action.result.msg);
                    }
                    else {
                        Ext.Msg.alert(orchid_lang.promptWindowInfo.errorTitle, orchid_lang.promptWindowInfo.updateFailureText);
                    }
                }
            });
        }
    }
});
